﻿using System.ComponentModel.Composition;
using System.ServiceModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;
using YunBox2.Contracts;

namespace YunBox2.Client.Infrastructure.ViewModels
{
    [Export]
    public class LogonMenuViewModel
    {
        private readonly IRegionManager _regionManager;
        private readonly ISystemService _systemService;
        private IEventAggregator _eventAggregator;
        private IFoundationService _foundationService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="regionManager"></param>
        /// <param name="eventAggregator"></param>
        [ImportingConstructor]
        public LogonMenuViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
        {
            _regionManager = regionManager;
            _eventAggregator = eventAggregator;

            _systemService = ClientContext.Current.SystemService;
            _foundationService = ClientContext.Current.FoundationService;


            FileMenuToInit();
            SystemMenuToInit();
            FoundationMenuToInit();
        }

        #region 文件菜单项

        private void FileMenuToInit()
        {
            _showMyProfileViewCommand = new DelegateCommand(ShowMyProfileView);
            _logoutCommand = new DelegateCommand(Logout);
            /*------------------------------------*/
            _shutdownCommand = new DelegateCommand(Shutdown);
        }

        #region 用户信息维护

        private DelegateCommand _showMyProfileViewCommand;

        public ICommand ShowMyProfileViewCommand
        {
            get
            {
                return _showMyProfileViewCommand;
            }
        }

        private void ShowMyProfileView()
        {
            _regionManager.RequestNavigate(ShellRegionNames.MainContentRegion, "MyProfilePage");
        }

        #endregion

        #region 注销当前用户

        private DelegateCommand _logoutCommand;

        public ICommand LogoutCommand
        {
            get
            {
                return _logoutCommand;
            }
        }

        private void Logout()
        {
            using (OperationContextScope cs = ClientContext.Current.CreateOperationContextScope(_systemService as IContextChannel))
            {
                if (_systemService.Logoff())
                {
                    ClientContext.Current.CurrentUser = null;
                }
            }
        }

        #endregion

        #region 退出程序

        private DelegateCommand _shutdownCommand;

        public ICommand ShutdownCommand
        {
            get
            {
                return _shutdownCommand;
            }
        }


        private void Shutdown()
        {
            try
            {
                using (OperationContextScope cs = ClientContext.Current.CreateOperationContextScope(_systemService as IContextChannel))
                {
                    _systemService.Logoff();
                }
            }
            finally
            {
                Application.Current.MainWindow.Close();
            }
        }

        #endregion

        #endregion

        #region 基本资料菜单项

        private void FoundationMenuToInit()
        {
            _showUOMPageCommand=new DelegateCommand(ShowUOMPage);
        }

        private void ShowUOMPage()
        {
            //var uc = ClientContext.Current.PrismContainer.GetExportedValue<UserControl>("UOMPage");


            //var rm = _regionManager.Regions[ShellRegionNames.MainContentRegion].Add(uc, "UOMEditor", true);
            

            //var vm = uc.DataContext as MainContentViewModel;

            //if (vm != null)
            //{
            //    vm.ScopeRegionManager = rm;
            //}
            ////_regionManager.Regions[ShellRegionNames.MainContentRegion].Activate(uc);

           _regionManager.RequestNavigate(ShellRegionNames.MainContentRegion,"UOMPage");
        }

        private DelegateCommand _showUOMPageCommand;
        public ICommand ShowUOMPageCommand
        {
            get
            {
                return _showUOMPageCommand;
            }
        }

        #endregion

        #region 系统管理菜单项

        private void SystemMenuToInit()
        {
        }

        #endregion
    }
}